package com.example.zzj.ziwuyundemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zzj.ziwuyundemo.R;
import com.example.zzj.ziwuyundemo.base.BaseActivity;
import com.example.zzj.ziwuyundemo.entity.ModifyPwdEntity;
import com.google.gson.Gson;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;


import java.io.IOException;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by zzj on 2016/11/29.
 */

public class ChangePassActivity extends BaseActivity implements View.OnClickListener {


    @InjectView(R.id.back)
    ImageView back;
    @InjectView(R.id.name_change_text)
    TextView nameChangeText;
    @InjectView(R.id.img_info)
    ImageView imgInfo;
    @InjectView(R.id.oldPwd)
    EditText oldPwd;
    @InjectView(R.id.newPwd)
    EditText newPwd;
    @InjectView(R.id.fixNewPwd)
    EditText fixNewPwd;
    @InjectView(R.id.fix)
    Button fix;

    private String userId;
    private String oldPass;
    private String newPass;
    private String fixNewPass;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_changepass);
        ButterKnife.inject(this);
        initView();
        initData();
    }

    private void initView() {
        Intent intent = getIntent();
        userId = intent.getStringExtra("userId");
        back.setVisibility(View.VISIBLE);
        nameChangeText.setVisibility(View.VISIBLE);
        imgInfo.setVisibility(View.VISIBLE);


    }

    private void initData() {
        back.setOnClickListener(this);
        fix.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.fix:
                oldPass = oldPwd.getText().toString();
                newPass = newPwd.getText().toString();
                fixNewPass = fixNewPwd.getText().toString();
                if (oldPass.length()<=0){
                    Toast.makeText(mContext,"旧密码不能为空",Toast.LENGTH_SHORT).show();
                }else {
                    if (newPass.length()<=0){
                        Toast.makeText(mContext,"新密码不能为空",Toast.LENGTH_SHORT).show();
                    }else{
                        if (newPass.equals(oldPass)){
                            Toast.makeText(mContext,"新旧密码不能相同,请重新输入！",Toast.LENGTH_SHORT).show();
                        }else {
                            if (fixNewPass.length()<=0){
                                Toast.makeText(mContext,"确定新密码不能为空",Toast.LENGTH_SHORT).show();
                            }else {
                                if (fixNewPass.equals(newPass)){
                                    OkHttpClient okHttpClient = new OkHttpClient();
                                    FormEncodingBuilder builder = new FormEncodingBuilder();
                                    builder.add("oldpwd", oldPass);
                                    builder.add("newpwd", fixNewPass);
                                    builder.add("userId", userId);
                                    final Request request = new Request.Builder()
                                            .url("http://192.168.1.75:8080/mclcm/a/mclcm/test/modifyPassword")
                                            .post(builder.build())
                                            .build();
                                    okHttpClient.newCall(request).enqueue(new Callback() {
                                        @Override
                                        public void onFailure(Request request, IOException e) {

                                        }

                                        @Override
                                        public void onResponse(Response response) throws IOException {
                                            String passBody = response.body().string();
                                            Log.d("passBody", passBody);
                                            Gson gson = new Gson();
                                            ModifyPwdEntity modifyPwdEntity = new ModifyPwdEntity();
                                            modifyPwdEntity = gson.fromJson(passBody,ModifyPwdEntity.class);
                                            final ModifyPwdEntity finalModifyPwdEntity = modifyPwdEntity;
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    String success = finalModifyPwdEntity.getPWD();
                                                    if (success.equals("success")){
                                                        Toast.makeText(mContext,"修改成功",Toast.LENGTH_SHORT).show();
                                                    }else{
                                                        Toast.makeText(mContext,"旧密码错误，请重新输入",Toast.LENGTH_SHORT).show();
                                                    }
                                                }
                                            });

                                        }
                                    });
                                }else {
                                    Toast.makeText(mContext,"两次输入的新密码不同",Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    }
                }
                break;
        }
    }
}
